package com.diandi.signature.service.impl.contract;


import com.alibaba.fastjson.JSONObject;
import com.diandi.signature.bean.cainfo.SignatureCAInfo;
import com.diandi.signature.bean.contract.SignatureContract;
import com.diandi.signature.bean.proceessinfo.SignatureProcessLog;
import com.diandi.signature.bean.runbatch.SignatureBatchRunInfo;
import com.diandi.signature.contract.*;
import com.diandi.signature.enums.fdd.CheckParamsType;
import com.diandi.signature.enums.fdd.ReturnCode;
import com.diandi.signature.enums.platfrom.*;
import com.diandi.signature.manager.CAInfo.SignatureCAManager;
import com.diandi.signature.manager.contract.SignatureContractManager;
import com.diandi.signature.mapper.SignatureBatchRunInfoMapper;
import com.diandi.signature.properties.FddProperties;
import com.diandi.signature.properties.SignatureProperties;
import com.diandi.signature.sign.SignUtil;
import com.diandi.signature.sign.Tools;
import com.diandi.signature.utils.CallServiceUtil;
import com.diandi.signature.utils.CheckParams;
import com.diandi.signature.utils.FddContractSignatureTools;
import com.diandi.signature.utils.PackageParams;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author mwz
* @create 2017-08-24 14:58
* @version V1.0.0
*/
@Service
public class SignatureContractServiceImpl implements SignatureContractService {

    private static Logger logger = LoggerFactory.getLogger(SignatureContractServiceImpl.class);

    //建立一个工作线程池
    public static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);

    @Autowired
    private SignatureProperties signatureProperties;

    @Autowired
    private FddProperties fddProperties;

    @Autowired
    private SignatureContractManager signatureContractManager;

    @Autowired
    private SignatureCAManager  signatureCAManager;

    @Autowired
    private FddArchivesContractService fddArchivesContractService;

    @Autowired
    private FddGenerateContractService fddGenerateContractService;

    @Autowired
    private FddSignatureContractService fddSignatureContractService;

    @Autowired
    private FddUploadContractService fddUploadContractService;

    @Autowired
    private SignatureBatchRunInfoMapper signatureBatchRunInfoManager;



    /**
     * 工作线程处理方法
     * @param platTradeNo
     * @param contract
     * @param params
     * @param filepath
     * @param queryGenerateMap
     */
    public void processSignature(final String platTradeNo, final SignatureContract contract, final Map<String, String> params, final String filepath, final Map queryGenerateMap) {

        fixedThreadPool.execute(new Runnable( ) {
            @Override
            public void run() {
                try {
                    logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 工作线程开始处理任务--");
                    contractTask(platTradeNo,contract,params,filepath,queryGenerateMap);
                    logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 工作线程完成处理任务--");
                } catch (Exception e) {
                    logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 工作线程处理处理报错"+e.getMessage()+"--");
                    e.printStackTrace();
                }
            }
        });
    }


    /**
     * 工作线程任务
     * @param platTradeNo
     * @param contract
     * @param params
     * @param filepath
     * @param queryGenerateMap
     * @return
     */
    public Map<String, String> contractTask( String platTradeNo,  SignatureContract contract, Map<String, String> params, String filepath,Map queryGenerateMap) {
        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 工作结程开始处理--");
            Map<String, String> returnMap = new HashMap<String, String>();
        try{

            /**
             * 6.1 合同上传
             */
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同上传]: 开始处理--");
            signatureContractManager.signatureUpdateProcessData(  contract, params , String.valueOf( InterfactType.合同上传.getValue() ));
            Map<String, String > queryUploadMap =  PackageParams.getUploadContractMapByParms(queryGenerateMap.get("contractNo").toString(),params.get("contractTitle"),filepath,platTradeNo);
            Map<String, String > rtnUploadMap = fddUploadContractService.FddUploadContract( contract,params,queryUploadMap, InterfactType.合同上传.getValue()  );
            if( !rtnUploadMap.get( "result").equals("success")){
                logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同上传]: 合同上传处理失败--");
                returnMap = Tools.createReturnParams(ReturnCode.err_1001, ReturnCode.err_1001.getMsg(), params.get("error"));
                return returnMap;
            }
            signatureContractManager.updateCompleteSignatureProcessData(  contract, params , String.valueOf( InterfactType.合同上传.getValue() ) ,rtnUploadMap);
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同上传]: 结束处理--");

            /**
             * 7.1 合同签章
             */
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同签章]: 开始处理--");

            Map<String, String > querySignatureMap = PackageParams.getContractSignatureMapByParms(queryGenerateMap.get("contractNo").toString(), params);
            Map<String, String > rtnSignaturedMap = new HashMap<String, String >();
            returnMap = fddSignatureContractService.fddSignatureContractProcess(contract,querySignatureMap,params,queryGenerateMap);
            String dUrl = "";
            String vUrl = "";
            if ("1000".equals(returnMap.get("code"))){
                dUrl = returnMap.get("download_url");
                vUrl = returnMap.get("viewpdf_url");
            }
            /*if(!"success".equals(returnMap.get("result"))){
                logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同签章]: 签章平台处理失败--");
                //记录中间状态
                returnMap = Tools.createReturnParams(ReturnCode.err_1001, ReturnCode.err_1001.getMsg(), params.get("error"));
                return returnMap;
            }*/
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同签章]: 完成处理--");

            /**
             * 8.1 合同归档
             */
            Map<String, String > rtnArchivesMap = new HashMap<String, String >();
            if ("1000".equals(returnMap.get("code"))){
                logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同归档]: 开始处理--");
                signatureContractManager.signatureUpdateProcessData(  contract, params , String.valueOf( InterfactType.合同归档.getValue() ));
                Map<String, String > queryArchivesMap = new HashMap<String, String >();
                queryArchivesMap.put("contractNo",contract.getContractCode());
                queryArchivesMap.put("platTradeNO",platTradeNo);
                returnMap = fddArchivesContractService.fddArchivesContract( queryArchivesMap );

                signatureContractManager.updateCompleteSignatureProcessData(  contract, params , String.valueOf( InterfactType.合同归档.getValue() ) ,returnMap);
                logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同归档]: 结束处理--");
            }

            /**
             * 9.1 处理返回给业务端的异步通知消息
             */
            Map<String, String > pkgMsgMap = new HashMap<String, String >();
            pkgMsgMap.put("responseCode",returnMap.get("code"));
            pkgMsgMap.put("responseMsg",returnMap.get("msg") );
            pkgMsgMap.put("contractNo", contract.getContractCode());
            if (StringUtils.isNotBlank(dUrl)){
                pkgMsgMap.put("downloadUrl", dUrl);
            }
            if (StringUtils.isNotBlank(vUrl)){
                pkgMsgMap.put("viewpdfUrl",vUrl);
            }
            try {
                String sign = SignUtil.signRSA(pkgMsgMap, signatureProperties.getPayPrivateSign(params.get("merchantNo")), signatureProperties.getCharset());
                pkgMsgMap.put("sign", sign);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String pgkMsg = Tools.createBusinessLinkString( pkgMsgMap,true,"utf-8");
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 回调开始处理"+params.get("notifyUrl")+"--");
            String result = CallServiceUtil.sendPost( params.get("notifyUrl"),pgkMsg);
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 回调结束处理result:"+result+"--");
            SignatureProcessLog signatureProcessLog = new SignatureProcessLog();
            if("SUCCESS".equals(result.toUpperCase())){
                 //更新合同表为完成，过程主表为完成
                signatureProcessLog.setBusinessStatus(BusinessStatus.异步回执成功.getStatus());
                signatureContractManager.notifySignatureUpdateProcessData(  contract,  String.valueOf( InterfactType.合同归档.getValue() ));
            }else{
                //接收失败
                //判断当前是否为最后一次请求
                signatureProcessLog.setBusinessStatus(BusinessStatus.异步回执失败.getStatus());
                //插入跑批表数据
                SignatureBatchRunInfo batchRunInfo = new SignatureBatchRunInfo();
                batchRunInfo.setSignatureCode(platTradeNo);
                batchRunInfo.setSignatureTrade("");
                if (params.containsKey("userId") && StringUtils.isNotBlank(params.get("userId"))){
                    batchRunInfo.setUserId(Long.parseLong(params.get("userId")));
                }
                batchRunInfo.setMerchantNo(params.get("merchantNo"));
                batchRunInfo.setPassCode(params.get("passCode"));
                batchRunInfo.setContractBatchCode(params.get("batchId"));
                batchRunInfo.setContractCode(querySignatureMap.get("contract_id"));
                batchRunInfo.setContractTemplateCode(params.get("contractType"));
                batchRunInfo.setContractTitle(params.get("contractTitle"));
                if (params.containsKey("borrowId") && StringUtils.isNotBlank(params.get("borrowId"))){
                    batchRunInfo.setBorrowId(Long.parseLong(params.get("borrowId")));
                }
                batchRunInfo.setInterfaceType(InterfactType.合同归档.getValue());
                batchRunInfo.setPgkMsg(JSONObject.toJSONString(params));
                batchRunInfo.setRetryTimes(3);
                batchRunInfo.setStatus(BatchRunStatus.异步通知初始化.getStatus());
                batchRunInfo.setNotifyUrl(params.get("notifyUrl"));

                signatureBatchRunInfoManager.insertSignatureBatchRunInfo(batchRunInfo);
            }

            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同渲染]: 完成返回给业务端--");

            /**更新返回给业务端的报文消息*/
            rtnSignaturedMap.put("tradeNo", platTradeNo);
            rtnSignaturedMap.put("returnMsg",returnMap.toString());
            rtnSignaturedMap.put("unnotifyMsg","");
            rtnSignaturedMap.put("passCode",params.get("passCode"));
            rtnSignaturedMap.put("userId",params.get("userId"));
            rtnSignaturedMap.put("downloadUrl", returnMap.get("download_url"));
            rtnSignaturedMap.put("viewpdfUrl", returnMap.get("viewpdf_url"));
            rtnSignaturedMap.put("contractCode", queryGenerateMap.get("contractNo").toString());
            if (returnMap.containsKey("code") && returnMap.get("code").equals("1000")){
                rtnSignaturedMap.put("status", String.valueOf(PkgMsgStatus.成功.getStatus()));
            } else {
                rtnSignaturedMap.put("status", String.valueOf(PkgMsgStatus.处理失败.getStatus()));
            }
            rtnSignaturedMap.put("returnMsg", returnMap.toString());
            signatureContractManager.updateRvcPkgMsgInfo(rtnSignaturedMap);

            //mongoService.insert("fddSign", returnMap.toString(), params, platTradeNo, Long.parseLong(params.get("userId")), returnMap.get("msg"), "应答报文");
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 完成工作线程处理--");
        }catch (Exception e ){
            e.printStackTrace();
            returnMap = Tools.createReturnParams(ReturnCode.err, "", null);
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 工作线程处理报错，结束处理"+e.getMessage()+"--");
            return returnMap;
        }
        return returnMap;
    }


    /**
     * 合同签章功能
     * @param request
     * @return
     */
    @Override
    public Map<String, String>  createContract(HttpServletRequest request,String platTradeNo) {
        /**
         * 1.1 验证业务端签名信息的正确性
         */
        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 开始业务平台验签");
        Map<String, String> returnMap = new HashMap<String, String>();
//        Map<String, String> params = Tools.checkSign(request, fddProperties, signatureProperties, "platform");
        Map<String, String> params = Tools.getParameterMap( request,false );
        if (params.containsKey("error")) {
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 业务平台验签失败--");
            returnMap = Tools.createReturnParams(ReturnCode.err_9001, ReturnCode.err_9001.getMsg(), params.get("error"));
            return returnMap;
        }
        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 业务平台验签成功--");

        //mongoService.insert("fddSign", "", params, platTradeNo, Long.parseLong(params.get("userId")), "", "请求报文");

        try{

            /**
             * 2.1 记录接收到的报文消息
             * */
            params.put("InterfaceType",String.valueOf( InterfactType.合同落地.getValue() ) );//InterfaceType
            params.put("contractCode",platTradeNo );
            signatureContractManager.insertRvcPkgMsgInfo(params);

            /**
             *  3.1 转化模板参数为Map
             */
            Map<String, String > rtnGenerateMap = new HashMap<String, String >();
            Map queryGenerateMap = FddContractSignatureTools.genContractParamMap(params.get("contractType"), params.get("contractParams").replaceAll(" ", ""));
            if (queryGenerateMap.containsKey("responseCode") && queryGenerateMap.get("responseCode").equals("ERR_LACK_PARAM")){
                return queryGenerateMap;
            }
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->结束转化模板参数为Map]: 模板参数："+queryGenerateMap.toString()+"--");

            /**
             *  4.1 验证CA信息的正确性,查询是否通过CA认证,未认证返回给业务端提示
             */
            SignatureCAInfo signatureCAInfo =  signatureCAManager.getSignatureCAInfo( params.get("userId"), params.get("passCode") );
            if ( null == signatureCAInfo ) {
                logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: CA未认证或CA数据示同步，处理中断返回--");

                returnMap = Tools.createReturnParams(ReturnCode.CA_3001,"", params.get("error"));
                return returnMap;
            }
            List<Map<String,String>> toBeQuery = null;
            if (ContractTypeAndTempName.借款协议.getType().equals(params.get("contractType"))){
                toBeQuery = (List<Map<String, String>>) queryGenerateMap.get("lenderInfoList");
            }
            if (ContractTypeAndTempName.债券转让协议.getType().equals(params.get("contractType"))){
                toBeQuery= (List<Map<String, String>>) queryGenerateMap.get("assignorList");
            }

            if (toBeQuery != null && !toBeQuery.isEmpty()){
                for (Map<String, String> assignor : toBeQuery) {
                    SignatureCAInfo assignorCa = signatureCAManager.getSignatureCAInfo(assignor.get("userId"), params.get("passCode"));
                    if ( null == assignorCa ) {
                        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: CA未认真或CA数据示同步，处理中断返回--");
                        //记录中间状态
                        returnMap = Tools.createReturnParams(ReturnCode.CA_3001,"", params.get("error"));
                        return returnMap;
                    }
                }
            }
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 完成CA认证验证--");

            /**
             *  4.2 合同、过程信息落地数据库
             */
            params.put("contractCode",queryGenerateMap.get("contractNo").toString());//合同号
            params.put("signatureCode",platTradeNo);//平台交易流水
            params.put("InterfaceType",String.valueOf( InterfactType.合同渲染.getValue() ) );
            SignatureContract contract = signatureContractManager.saveSignatureProcess(params);
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 完成合同、过程信息落地数据库--");

            /**
             *  5.1 合同渲染处理
             */
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同渲染]: 开始合同渲染--");
            /*更新为正在处理*/
            signatureContractManager.signatureUpdateProcessData(  contract, params , String.valueOf( InterfactType.合同渲染.getValue() ) );
            queryGenerateMap.put("templatePath", ContractTypeAndTempName.getEnum(params.get("contractType")).getTempName());
            queryGenerateMap.put("platCustomerId", fddProperties.getPlatCustomerId());
            rtnGenerateMap = fddGenerateContractService.fddGenerateContract( queryGenerateMap );
            if( !rtnGenerateMap.get( "responseCode").equals("0000")){
                logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同渲染]: 合同渲染处理失败--");
                //记录中间状态
                //TODO
                signatureContractManager.signatureErrorUpdateData(  contract, params );
                returnMap = Tools.createReturnParams(ReturnCode.QZ_7006, "", params.get("error"));
                return returnMap;
            }
            /** 渲染合同完成，更新数据状态  */
            boolean nlfag = signatureContractManager.updateCompleteSignatureProcessData(  contract, params , String.valueOf( InterfactType.合同渲染.getValue() ),rtnGenerateMap);
            if( !nlfag){
                logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同渲染]: 合同渲染处理失败--");
                signatureContractManager.signatureErrorUpdateData(  contract, params );
                returnMap = Tools.createReturnParams(ReturnCode.QZ_7006, "", params.get("error"));
                return returnMap;
            }
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成->合同渲染]: 完成处理--");


            /**
             *  5.1 合同上传、合同签章交给工作线程来处理
             */
            processSignature( platTradeNo,contract, params , rtnGenerateMap.get("file"),queryGenerateMap );

            /**
             * 6.1 业务同步回执信息
             */
            returnMap.put("responseCode", "0000");
            returnMap.put("responseMsg",ProcessFlag.处理中.getTypeName());
            returnMap.put("contractNo", queryGenerateMap.get("contractNo").toString());
//            returnMap.put("downloadUrl", "");
//            returnMap.put("viewpdfUrl", "");

           // signatureContractManager.updateRvcPkgMsgInfo(returnMap);

           // mongoService.insert("fddSign", returnMap.toString(), params, platTradeNo, Long.parseLong(params.get("userId")), returnMap.get("msg"), "应答报文");

            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 完成处理--");
        }catch (Exception e ){
            e.printStackTrace();
            returnMap = Tools.createReturnParams(ReturnCode.err, "", null);
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同生成]: 结束处理--");
            return returnMap;
        }
        return returnMap;
    }


    @Override
    public Map<String, String> QueryContract(HttpServletRequest request,String platTradeNo ) {
        /**
         * 1.1 验证业务端签名信息的正确性
         */
        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 开始业务平台验签");
        Map<String, String> returnMap = new HashMap<String, String>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
//        Map<String, String> params = Tools.checkSign(request, fddProperties, signatureProperties, "platform");
        Map<String, String> params = Tools.getParameterMap( request,false );
        if (params.containsKey("error")) {
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 业务平台验签失败--");
            returnMap = Tools.createReturnParams(ReturnCode.err_9001, ReturnCode.err_9001.getMsg(), params.get("error"));
            return returnMap;
        }
        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 业务平台验签成功--");

        /**
         * 3.1 封装要进行校验的参数
         */
        List<Object[]> checkList = new ArrayList<Object[]>();
        if (params.containsKey("accountType"))
            checkList.add(new Object[]{"accountType", params.get("accountType"), CheckParamsType.枚举校验.getType(), "AccountType"});
        /**
         * 3.2 调用校验方法
         */
        returnMap = CheckParams.checkParams(checkList);
        if (null != returnMap && !returnMap.isEmpty()) {
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 业务平台参数值错误--");
            return returnMap;
        }
        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 校验业务平台参数值正确--");
        /**
         * 4.1 查询合同信息
         */

        queryMap.put("userId", params.get("userId"));
        String passCode = params.get("passCode");
        queryMap.put("passCode", passCode);
        String contractNo = params.get("contractNo");
        queryMap.put("contractNo", contractNo);
        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 获取数据从数据表中--");
        SignatureContract signatureContract =  signatureContractManager.getSignatureContractByCode( queryMap );
        if (null != signatureContract) {
//            returnMap = MapTransf.objrctTransfMap(signatureContract);
            returnMap.put("returnUlr", signatureContract.getContractViewUrl());
            returnMap.put("downUrl", signatureContract.getContractDownloadUrl());
            returnMap.put("status", String.valueOf(signatureContract.getStatus()));
            returnMap.put("contractNo", signatureContract.getContractCode());
            returnMap.putAll(Tools.createReturnParams(ReturnCode.ok, ReturnCode.ok.getMsg(), null));
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 组织好数据结果--");
        } else {
            logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同查询]: 无数据，组织好数据结果返回给业务端--");
            returnMap = Tools.createReturnParams(ReturnCode.err_2999, ReturnCode.err_2999.getMsg(), null);
        }
        return returnMap;
    }

    /**
     * 回调处理
     *  1、针对返回的报文进行验签
     *  2、记录中间状态，方便业务操作失败，批量业务来处理
     *  3、归档合同
     *  4、更新记录信息
     * @param request
     * @return
     */
    @Override
    public String ContractCallBack( String platTradeNo, HttpServletRequest request) {

        logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同签章-合同回调]: 开始合同回调处理--");
        Map<String, String> returnMap = new HashMap<String, String>();
        Map<String, String> requestMap = Tools.getParameterMap( request,false );

        try{
            /**
             * 2.1 验签,如果失败直接返回给法，
             * 几次错误后，交给定时任务来处理
             */
            if (!Tools.checkFddSign( requestMap.get("msg_digest"),requestMap.get("transaction_id"),requestMap.get("timestamp"))){
                logger.info("收到业务平台[合同创建]请求消息其中参数"+returnMap.toString()+"不能为空,返回给业务端,结束处理");
                return "error";
            }

            /**3.1 更新异步回调信息*/
            requestMap.put( "returnMsg",requestMap.toString() );
            signatureContractManager.updateSendPkgMsgInfo( requestMap );


            Map<String,Object> queryMap = new HashMap<String, Object>();
            queryMap.put("contract_code",requestMap.get("transaction_id"));
            SignatureContract contract = signatureContractManager.getSignatureContractByCode( queryMap );
            if( contract == null ){
                logger.info("--rcv:platTradeNo:"+platTradeNo+":[合同签章-合同回调]: 未全部完成签章，结束合同回调处理--");
                return "success";
            }

            /**3.2 更新回调信息状态信息,记录中间状态信息*/
            //requestMap.put( "returnMsg",requestMap.toString() );
            //signatureContractManager.updateSendPkgMsgInfo( requestMap );

            //更新合同明细
            Map<String, String> updateContractMap = new HashMap<String, String>();
            updateContractMap.put("signatureCode", requestMap.get("transaction_id"));
            updateContractMap.put("unnotifyMsg", requestMap.toString());
            if (requestMap.get("result_code").equals("3000")){
                //签章成功
                updateContractMap.put("status", String.valueOf(ProcessFlag.处理完成.getType()));
            } else if (requestMap.get("result_code").equals("3001")){
                //签章失败
                updateContractMap.put("status", String.valueOf(ProcessFlag.处理失败.getType()));
            }
            signatureContractManager.updateBySignatureCode(updateContractMap);

            return "success";

        }catch ( Exception e) {

        }
        return "success";
    }

    @Override
    public List<SignatureContract> queryTask() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        Date now_10 = new Date(new Date().getTime() - 10000);
        paramMap.put("time", now_10);
        return signatureContractManager.queryTask(paramMap);
    }

    public Map<String,String> getContractMap(  Map<String, String> params){
        Map<String, String> contractMap = new HashMap<String, String>();
        contractMap.put("platTradeNo",params.get("platTradeNo"));
        contractMap.put("contractNo",params.get("contractNo"));
        contractMap.put("userId",params.get("userId"));
        contractMap.put("templateCode",params.get("templateCode"));
        contractMap.put("borrowId",params.get("borrowId"));
        contractMap.put("passCode",params.get("passCode"));
        contractMap.put("merchantNo",params.get("merchantNo"));
        contractMap.put("contractTitle",params.get("contractTitle"));
        contractMap.put("contractTitle",params.get("contractTitle"));
        return contractMap;
    }
}
